File access sample


Windows Runtime, Windows 8.1, Windows Phone 8.1
Storage, universal app
Windows RT
en-US
4/2/2014

This sample shows how to create, read, write, copy and delete a file, how to retrieve file properties, and how to track a file or folder so that your app can access it again. This sample uses Windows.Storage and Windows.Storage.AccessCache API.

Note  This sample was created using one of the universal app templates available in Visual Studio. It shows how its solution is structured so it can run on both Windows 8.1 and Windows Phone 8.1. For more info about how to build apps that target Windows and Windows Phone with Visual Studio, see Build apps that target Windows and Windows Phone 8.1 by using Visual Studio.

The sample demonstrates these tasks:

  1. Create a file in the Pictures library

    Uses one of the StorageFolder.CreateFileAsync methods to create the file.

  2. Get a file's parent folder

    (Not supported for Windows Phone.)

    Uses the GetParentAsync method to get the parent folder of the file that was created in the Picture folder. The app has the Pictures library capability, so it can access the folder where the file was created.

  3. Write and read text in a file

    Uses the FileIO.WriteTextAsync and FileIO.ReadTextAsync methods to write and read the file. For a walkthrough of this task, see Quickstart: reading and writing a file.

  4. Write and read bytes in a file

    Uses the FileIO.WriteBufferAsync and FileIO.ReadBufferAsync methods to write and read the file. For a walkthrough of this task, see Quickstart: reading and writing a file.

  5. Write and read a file using a stream

    Uses the following API to write and read the file using a stream.

    For a walkthrough of this task, see Quickstart: reading and writing a file.

  6. Display file properties

    Uses the StorageFile.GetBasicPropertiesAsync method and the StorageFile.Properties property to get the properties of the file.

  7. Track a file or folder so that you can access it later (persisting access)

    Uses the StorageApplicationPermissions.FutureAccessList and StorageApplicationPermissions.MostRecentlyUsedList properties to remember a file or folder so that it can be accessed later.

    For a walkthrough of this task, see How to track recently used files and folders.

  8. Copy a file

    Uses one of the StorageFile.CopyAsync methods to copy the file.

  9. Compare two files to see if they're the same

    (Not supported for Windows Phone.)

    Uses the StorageFile.IsEqual method to compare two files.

  10. Delete a file

    Uses one of the StorageFile.DeleteAsync methods to delete the file.

  11. Try to get a file without getting an error

    (Not supported for Windows Phone.)

    Uses the StorageFolder.TryGetItemAsync method to get a file without raising an exception.

Note  If you want to learn about accessing files using a file picker, see Quickstart: Accessing files with file pickers.

To obtain an evaluation copy of Windows 8.1, go to Windows 8.1.

To obtain an evaluation copy of Microsoft Visual Studio 2013, go to Visual Studio 2013.

Note  For Windows 8 app samples, download the Windows 8 app samples pack. The samples in the Windows 8 app samples pack will build and run only on Microsoft Visual Studio 2012.

Related topics

Windows 8 app samples
Related samples
File picker sample
Folder enumeration sample
Programmatic file search sample
File and folder thumbnail sample

Related technologies

Windows.Storage namespace, Windows.Storage.AccessCache namespace, Windows.Storage.FileProperties, Windows.Storage.Streams namespace

Operating system requirements

Client
Windows 8.1
Server
Windows Server 2012 R2
Phone
Windows Phone 8.1

Build the sample

  1. Start Microsoft Visual Studio 2013 Update 2 and select File > Open > Project/Solution.
  2. Go to the directory to which you unzipped the sample. Then go to the subdirectory named for the sample and double-click the Visual Studio 2013 Update 2 Solution (.sln) file.
  3. Follow the steps for the version of the sample you want:
    • To build the Windows version of the sample:

      1. Select FileAccess.Windows in Solution Explorer.
      2. Press Ctrl+Shift+B, or use Build > Build Solution, or use Build > Build FileAccess.Windows.
    • To build the Windows Phone version of the sample:

      1. Select FileAccess.WindowsPhone in Solution Explorer.
      2. Press Ctrl+Shift+B or use Build > Build Solution or use Build > Build FileAccess.WindowsPhone.

Run the sample

  • To deploy and run the Windows version of the sample:

    1. Right-click FileAccess.Windows in Solution Explorer and select Set as StartUp Project.
    2. To debug the sample and then run it, press F5 or use Debug > Start Debugging. To run the sample without debugging, press Ctrl+F5 or use Debug > Start Without Debugging.
  • To deploy and run the Windows Phone version of the sample:

    1. Right-click FileAccess.WindowsPhone in Solution Explorer and select Set as StartUp Project.
    2. To debug the sample and then run it, press F5 or use Debug > Start Debugging. To run the sample without debugging, press Ctrl+F5 or use Debug > Start Without Debugging.